﻿var s_width=0;//控制变量
var img_width=$(window).width();//图片宽度，每次滚动距离
var img_num=5;//图片数量
var max_width=-(img_width*img_num);//滚动宽度
var speed=3000;//定时器时间间隔

var num=0;//当前图片编号
var pic=document.getElementById("imgbox");
var mebtnopenurl = 'http://yingxiaosishu.com/weixin/#box/mobile/gifts/recome.php';
var timeLineLink = 'http://yingxiaosishu.com/weixin/#box/mobile/gifts/recome.php';
$(function(){
	//给手机盒子设定宽度
	$("#imgbox").width(img_width*img_num*2);
	$("#contimg li").width(img_width);
	
	$("#contimg2").html($("#contimg").html());
	interval=setInterval(run, speed);
	
	touches(pic,"swiperight",function(){//向右触摸
		clearInterval(interval);
		$("#num"+num).css("background-color","#fff");
		if(s_width==0){
			$("#imgbox").css("marginLeft",max_width);
			s_width=max_width;	
		}
		s_width+=img_width;
		$("#imgbox").animate({marginLeft:s_width+"px"},200);
		if(num==0){
			num=4;
		}else{
			num--;
		}
		$("#num"+num).css("background-color","yellow");
		interval=setInterval(run, speed);
	});
	
	touches(pic,"swipeleft",function(){//向左触摸
		clearInterval(interval);
		$("#num"+num).css("background-color","#fff");
		if(s_width<=max_width){
			$("#imgbox").css("marginLeft","0px");
			s_width=0;	
		}
		s_width-=img_width;
		$("#imgbox").animate({marginLeft:s_width+"px"},200);
		if(num==4){
			num=0;
		}else{
			num++;
		}
		$("#num"+num).css("background-color","yellow");
		interval=setInterval(run, speed);	
	});
	
	addLoadEvent(preloader);//预加载
})

function touches(obj,direction,fun){
	//obj:ID对象
	//direction:swipeleft,swiperight,swipetop,swipedown,singleTap,touchstart,touchmove,touchend
	//           划左，     划右，     划上，     划下，     点击，    开始触摸，   触摸移动， 触摸结束
	//fun:回调函数
	var defaults={x:5,y:5,ox:0,oy:0,nx:0,ny:0};
	direction=direction.toLowerCase();
	//配置：划的范围在5X5像素内当点击处理
	obj.addEventListener("touchstart",function() {
		defaults.ox=event.targetTouches[0].pageX;
		defaults.oy=event.targetTouches[0].pageY;
		defaults.nx=defaults.ox;
		defaults.ny=defaults.oy;
		if(direction.indexOf("touchstart")!=-1)fun();
	}, false);
	obj.addEventListener("touchmove",function() {
		event.preventDefault();
		defaults.nx=event.targetTouches[0].pageX;
		defaults.ny=event.targetTouches[0].pageY;
		if(direction.indexOf("touchmove")!=-1)fun();
	}, false);
	obj.addEventListener("touchend",function() {
		var changeY=defaults.oy - defaults.ny;
		var changeX=defaults.ox - defaults.nx;
		if(Math.abs(changeX)>Math.abs(changeY)&&Math.abs(changeY)>defaults.y){
			//左右事件
			if(changeX > 0) {
				if(direction.indexOf("swipeleft")!=-1)fun();
			}else{
				if(direction.indexOf("swiperight")!=-1)fun();
			}
		}else if(Math.abs(changeY)>Math.abs(changeX)&&Math.abs(changeX)>defaults.x){
			//上下事件
			if(changeY > 0) {
				if(direction.indexOf("swipetop")!=-1)fun();
			}else{
				if(direction.indexOf("swipedown")!=-1)fun();
			}
		}else{
			//点击事件
			if(direction.indexOf("singleTap")!=-1)fun();
		}
		if(direction.indexOf("touchend")!=-1)fun();
	}, false);
}


function run(){//图片滚动事件
	if(s_width<=max_width){
		$("#imgbox").css("marginLeft","0px");
		s_width=0;
	}
	$("#num"+num).css("background-color","#fff");
	s_width-=img_width;
	$("#imgbox").animate({marginLeft:s_width+"px"},500);
	if(num==4){
		num=0;
	}else{
		num++;
	}
	$("#num"+num).css("background-color","yellow");
}

//预加载
function preloader(){
    if(document.images){
        var img1=new Image();
        var img2=new Image();
        var img3=new Image();
		var img4=new Image();
        var img5=new Image();
        img1.src="template/fuzi/images/box1.png";
        img2.src="template/fuzi/images/box2.png";
        img3.src="template/fuzi/images/box3.png";
		img4.src="template/fuzi/images/box4.png";
        img5.src="template/fuzi/images/box5.png";
    }
}
function addLoadEvent(func){
    var oldonload=window.onload;
    if (typeof window.onload!='function'){
        window.onload=func;
    }else{
        window.onload=function(){
            if(oldonload){
                oldonload();
            }
            func();
        }
    }
}